var gulp = require('gulp');
var sass = require('gulp-sass');
var rename = require('gulp-rename');
var cleanCss = require('gulp-clean-css');
var useref = require('gulp-useref');
var gulpif = require('gulp-if');
var uglify = require('gulp-uglify');
var ngAnnotate = require('gulp-ng-annotate');
var concat = require('gulp-concat');
var notify = require("gulp-notify");
var templateCache = require('gulp-angular-templatecache');
var htmlmin = require('gulp-htmlmin');
var autoprefix = require('gulp-autoprefixer');
var sourcemaps = require('gulp-sourcemaps');
var order = require('gulp-order');
var imagemin = require('gulp-imagemin');
var minimist = require('minimist');
var replace = require('gulp-replace');
var rev = require('gulp-rev');
var revReplace = require('gulp-rev-replace');

var knownOptions = {
    string: 'env',
    default: {env: process.env.NODE_ENV || 'prd', lang: 'zh_CN'}
};
var options = minimist(process.argv.slice(2), knownOptions);

// 源代码目录
var src_paths_shared = {
    sass: ['src/shared/scss/*.scss'],
    css: ['src/shared/css/*.css'],
    font: ['src/shared/css/font/**'],
    img: ['src/shared/img/**'],
    lib: ['src/shared/lib/**'],
    shared_html: ['src/shared/template/*.html'],
    shared_js: ['src/shared/scripts/**/*', '!src/shared/scripts/shared.js']
};

// 中间目录（sass实时编译为css，html模板实时编译为template）
var dest_paths_shared = {
    sass: 'src/shared/css/',
    shared_html: 'src/shared/scripts/',
    shared_js: 'src/shared/scripts/'
};

// 发布目录（build打包结果）
var dist_paths_shared = {
    css: 'dist/' + options.lang + '/shared/css/',
    font: 'dist/' + options.lang + '/shared/css/font/',
    img: 'dist/' + options.lang + '/shared/img/',
    lib: 'dist/' + options.lang + '/shared/lib/',
    shared_js: 'dist/' + options.lang + '/shared/scripts/'
};

gulp.task('shared-watch', false, function () {
    gulp.watch(src_paths_shared.sass, ['shared-sass']);
    gulp.watch(src_paths_shared.shared_html, ['shared-template-cache']);
    gulp.watch(src_paths_shared.shared_js, ['shared-scripts-concat']);
});

gulp.task('shared-sass', false, function (done) {
    gulp.src(src_paths_shared.sass)
        .pipe(sass())
        .on('error', sass.logError)
        .pipe(autoprefix({browsers: ['last 2 versions']}))
        .pipe(gulp.dest(dest_paths_shared.sass))
        .on('end', done);
});

gulp.task('shared-template-cache', false, function (done) {
    gulp.src(src_paths_shared.shared_html)
        .pipe(templateCache({module: 'servicecenter-shared'}))
        .pipe(gulp.dest(dest_paths_shared.shared_html))
        .on('end', done);
});

//构建通用js(注意JS顺序: 需要优先处理的js在order中定义，已经定义的顺序不能改变)
gulp.task('shared-scripts-concat', 'shared模块所有js合并为shared.js', function (done) {
    gulp.src(['src/shared/scripts/**/*.js', '!src/shared/scripts/shared.js'])
        .pipe(order([
            'src/shared/scripts/system.js',
            'src/shared/scripts/culture/*.js',
            'src/shared/scripts/system-init.js',

            'src/shared/scripts/**/*.js'
        ], {
            base: './'
        }))
        .pipe(concat('shared.js'))
        .pipe(ngAnnotate({
            single_quotes: true
        }))
        .pipe(gulp.dest(dest_paths_shared.shared_js))
        .on('end', done);
});

gulp.task('font', '字体文件copy到dist', function(done) {
    gulp.src(src_paths_shared.font)
        .pipe(gulp.dest(dist_paths_shared.font))
        .on('end', done);
});

gulp.task('img', '图片压缩处理', function(done) {
    gulp.src(src_paths_shared.img)
        .pipe(imagemin())
        .pipe(gulp.dest(dist_paths_shared.img))
        .on('end', done);
});

gulp.task('lib', 'lib copy到dist', function(done) {
    gulp.src(src_paths_shared.lib)
        .pipe(gulp.dest(dist_paths_shared.lib))
        .on('end', done);
});

gulp.task('shared-styles', '公共css压缩、替换en图片目录', ['font', 'img', 'shared-sass'], function (done) {
    gulp.src(dest_paths_shared.sass + '*')
        .pipe(cleanCss())
        .pipe(gulp.dest(dist_paths_shared.css))
        .on('end', done);
});

gulp.task('shared-scripts', 'shared.js文件压缩、英文环境替换SYSCONF常量', ['shared-scripts-concat', 'lib'], function (done) {
    gulp.src(['src/shared/scripts//shared.js'])
        .pipe(sourcemaps.init())
        .pipe(uglify())
        .pipe(sourcemaps.write('./', {
            sourceRoot: './'
        }))
        .pipe(gulpif(options.lang === 'en_US', replace(/zh/, 'en'))) //英文环境，替换SYSCONF.lang: 'en'
        .pipe(gulp.dest(dist_paths_shared.shared_js))
        .on('end', done);
});

gulp.task('build-shared', 'shared模块编译任务', ['shared-scripts','shared-styles'], function() {

});

gulp.task('task-shared', 'shared模块监听任务', ['shared-watch'], function () {

});
